﻿using System;
using System.Collections;
using System.Linq;
using System.Text;
using Meebey.SmartIrc4net;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace IRCBot
{
    class Program
    {
        private IrcClient irc = new IrcClient();
        private string server = "irc.se.quakenet.org";
        private int port = 6667;
        //private string channel = "#tf2connexion.test";

        private Dictionary<String, Channel> channels = new Dictionary<String, Channel>(){
            {"#tf2connexion.test",new Channel(){Id=1,Name="#tf2connexion.test",Pickups=new List<Pickup>()}}
        };


        public static void Main(string[] args)
        {
            Program prog = new Program();
        }

        public Program()
        {
            irc.OnConnected += new EventHandler(OnConnected);
            irc.OnPing += new PingEventHandler(OnPing);
            irc.OnChannelMessage += new IrcEventHandler(OnChannelMessage);
            irc.OnQueryMessage += new IrcEventHandler(OnQueryMessage);

            try
            {
                irc.ActiveChannelSyncing = true;
                irc.Connect(server, port);
            }
            catch (Exception e)
            {
                Console.Write("Failed to connect:n" + e.Message);
                Console.ReadKey();
            }
        }

        void OnConnected(object sender, EventArgs e)
        {
            irc.Login("TF2cBot", "CSharp Bot", 0, "TF2cBot");
            foreach (Channel channel in channels.Values)
            {
                irc.RfcJoin(channel.Name);
            };
            irc.Listen();
        }

        void OnPing(object sender, PingEventArgs e)
        {
            Console.WriteLine("Responded to ping at {0}.", DateTime.Now.ToShortTimeString());
        }

        void OnChannelMessage(object sender, IrcEventArgs e)
        {
            ChannelUser user = irc.GetChannelUser(e.Data.Channel, e.Data.Nick);
            Channel channel = channels[e.Data.Channel];

            Regex regex = new Regex("^!new(\\s.+)?");
            if (regex.IsMatch(e.Data.Message.ToLower()))
            {
                if (user.IsOp)
                {
                    Match result = regex.Match(e.Data.Message.ToLower());
                    if (result.Groups.Count > 1)
                    {
                        PickupType pickupType;
                        if (Enum.TryParse<PickupType>(result.Groups[1].Captures[0].Value,out pickupType))
                        {                            
                            Pickup newPickup = Pickup.createPickup(pickupType);

                                                        
                            channel.Pickups.Add(newPickup);
                        }
                        else
                        {
                            irc.RfcNotice(user.Nick, "Type of pickup not reconize");
                        }
                    }
                    else
                    {
                        irc.RfcNotice(user.Nick, "You don't use correct syntax. Use !new PickupType");
                    }
                    
                    
                }
                else
                    irc.RfcNotice(user.Nick, "You're not allowed to use this command");
                //Console.WriteLine("<{0}> is not allowed",user.Nick);

            }

            if (e.Data.Message.ToLower().StartsWith("!add"))
            {

            }
            else
            {

            }

            Console.WriteLine(e.Data.Type + ":");
            Console.WriteLine("(" + e.Data.Channel + ") <" + e.Data.Nick + "> " + e.Data.Message);
        }

        void OnQueryMessage(object sender, IrcEventArgs e)
        {
            Console.WriteLine(e.Data.Type + ":");
            Console.WriteLine("(private) <" + e.Data.Nick + "> " + e.Data.Message);
        }
    }
}
